#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from sale import *
import cgi

form = cgi.FieldStorage()
msg=""

print u"Content-type: text/plain"
print

if form.has_key(u"session"): 
  session = form[u"session"].value

if form.has_key(u"bill_id"): 
  bill_id = form[u"bill_id"].value
else:
  msg+=u"<br/>***Manque id commande !***"

if form.has_key(u"name"): 
  name = form[u"name"].value
else:
  msg+=u"<br/>***Manque name commande !***"

if form.has_key(u"address"): 
  address = form[u"address"].value
else:
  msg+=u"<br/>***Manque address commande !***"

if form.has_key(u"email"): 
  email = form[u"email"].value
else:
  msg+=u"<br/>***Manque email commande !***"

if form.has_key(u"phone"): 
  phone = form[u"phone"].value
else:
  msg+=u"<br/>***Manque phone commande !***"

if form.has_key(u"note"): 
  note = form[u"note"].value
else:
  msg+=u"<br/>***Manque note commande !***"  


# CONFIRM BILL
sale=Sale()

if sale.updateCustomer( bill_id, name, address, email, phone, note ) == 1 :
  bill=sale.read(bill_id)
  ## TODO creer un user et envoyer un email de confirmation...
  ## Decompter les produit vendu du stock....pas ici dans getProductPayment...
  item_name="Commande reference : %s/%s" % ( name, bill_id )
  print "<br/><b>PAYER VOTRE COMMANDE</b><br/><br/><form action='https://www.paypal.com/cgi-bin/webscr' method='post'><input type='hidden' name='cmd' value='_s-xclick'><input type='hidden' name='hosted_button_id' value='NVED8SHFCNDN2'><input type='hidden' name='item_name' value='%s'><input type='hidden' name='item_number' value='%s'><input type='hidden' name='amount' value='%s'><input type='hidden' name='notify_url' value='%s'><input type='hidden' name='business' value='%s'><input type='hidden' name='shipping' value='0.00'><input type='hidden' name='currency_code' value='EUR'><input type='hidden' name='lc' value='FR'><input type='image' src='https://www.paypal.com/fr_FR/FR/i/btn/btn_paynowCC_LG.gif' border='0' name='submit' alt='PayPal - la solution de paiement en ligne la plus simple et la plus sécurisée !'><img alt='' border='0' src='https://www.paypal.com/fr_FR/i/scr/pixel.gif' width='1' height='1'></form>" % ( item_name, bill_id, bill[u'sale_price'], "http://comics.awardspace.biz/notifyPaypal.py", "COMICS CMS - comics@comics.awardspace.biz")

else:
  msg+=u"<br/>Erreur impossible de finaliser la commande !"
  print msg
